const { ipcRenderer } = require("electron");
let port = null; // 存储主进程传来的端口

document.querySelector("#start-btn").addEventListener("click", () => {
  // 请求主进程把端口返回
  ipcRenderer.postMessage("request-port", null, []);
});

const showEle = document.querySelector("#data-display");

ipcRenderer.on("deliver-port", (event) => {
  port = event.ports[0];
  // 给主进程发送消息
  port.postMessage("start");
  port.onmessage = (event) => {
    showEle.innerHTML = event.data;
  };
});
document.querySelector("#stop-btn").addEventListener("click", () => {
  port.close();
  showEle.innerHTML = "已经结束";
  port = null;
});
